package com.ctshk.rpc.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.hotel.dto.HotelInitializationSettingsChannelDTO;
import com.ctshk.rpc.hotel.dto.HotelInitializationSettingsDTO;
import com.ctshk.rpc.hotel.entity.HotelInitializationSettings;
import com.ctshk.rpc.hotel.entity.HotelInitializationSettingsChannel;
import com.ctshk.rpc.hotel.mapper.HotelInitializationSettingsChannelMapper;
import com.ctshk.rpc.hotel.mapper.HotelInitializationSettingsMapper;
import com.ctshk.rpc.hotel.req.HotelInitializationSettingsChannelReq;
import com.ctshk.rpc.hotel.req.HotelInitializationSettingsReq;
import com.ctshk.rpc.hotel.service.IHotelInitializationSettingsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 酒店api初始化设置表 服务实现类
 * </p>
 *
 * @author yinhuan
 * @since 2021-03-03
 */
@DubboService
public class HotelInitializationSettingsServiceImpl extends ServiceImpl<HotelInitializationSettingsMapper, HotelInitializationSettings> implements IHotelInitializationSettingsService {
    @Autowired
    private HotelInitializationSettingsChannelMapper hotelInitializationSettingsChannelMapper;
    /**  0-未删除  1-已删除*/
    public final static Integer NOT_DELETE = 0;
    public final static Integer IS_DELETED = 1;
    public final static Integer ONOFF = 1;
    public final static Integer ONLINE = 2;
    /**
     * 保存巴士票api设置
     *
     * @param hotelInitializationSettingsReq
     */
    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public Result saveSettingInfo(HotelInitializationSettingsReq hotelInitializationSettingsReq, TokenUser user) {
        if(user == null){
            return Result.failed(SystemError.USER_1000);
        }
        if (hotelInitializationSettingsReq == null) {
            return Result.failed(SystemError.BUS_PRODUCT_9002);
        }
        HotelInitializationSettings hotelInitializationSettings = new HotelInitializationSettings();

        //根据供应商id查询初始默认的酒店api是否存在
        List<HotelInitializationSettings> busInitializationSettingsList = this.baseMapper.selectList(new QueryWrapper<HotelInitializationSettings>().lambda()
                .eq(HotelInitializationSettings::getSupplierId, hotelInitializationSettingsReq.getSupplierId())
                .eq(HotelInitializationSettings::getIsDeleted,NOT_DELETE));

        if (CollectionUtils.isNotEmpty(busInitializationSettingsList)) {
            //存在修改
            hotelInitializationSettings = busInitializationSettingsList.get(0);
            BeanUtils.copyProperties(hotelInitializationSettingsReq, hotelInitializationSettings);
            hotelInitializationSettings.setModifiedId(user.getId());
            hotelInitializationSettings.setGmtModified(LocalDateTime.now());
            this.baseMapper.updateById(hotelInitializationSettings);
            //删除旧的上架渠道
            hotelInitializationSettingsChannelMapper.delete(Wrappers.<HotelInitializationSettingsChannel>lambdaQuery().eq(HotelInitializationSettingsChannel::getSettingId, hotelInitializationSettings.getId()));
            //保存渠道信息
            List<HotelInitializationSettingsChannelReq> hotelInitializationSettingsReqList = hotelInitializationSettingsReq.getHotelInitializationSettingsChannelReq();
            if(hotelInitializationSettingsReq.getHotelInitializationSettingsChannelReq().size()>0){
                for (int i = 0; i < hotelInitializationSettingsReq.getHotelInitializationSettingsChannelReq().size(); i++) {
                    HotelInitializationSettingsChannel hotelInitializationSettingsChannels = EntityUtil.copy(hotelInitializationSettingsReq.getHotelInitializationSettingsChannelReq().get(i), HotelInitializationSettingsChannel.class);
                    hotelInitializationSettingsChannels.setId(SnowflakeIdWorker.nextId());
                    hotelInitializationSettingsChannels.setSettingId(hotelInitializationSettings.getId());
                    if(hotelInitializationSettingsChannels.getChannelName().equals("自營門市")||hotelInitializationSettingsChannels.getChannelName().equals("代理商")){
                        hotelInitializationSettingsChannels.setChannelType(ONOFF);
                    }else if(hotelInitializationSettingsChannels.getChannelName().equals("第三方平台")||hotelInitializationSettingsChannels.getChannelName().equals("官方平台")){
                        hotelInitializationSettingsChannels.setChannelType(ONLINE);
                    }
                    hotelInitializationSettingsChannels.setIsDeleted(IsDeletedCode.NO.getCode());
                    hotelInitializationSettingsChannels.setGmtCreate(LocalDateTime.now());
                    hotelInitializationSettingsChannelMapper.insert(hotelInitializationSettingsChannels);
                }
            }
        } else {
            //不存在新增
            BeanUtils.copyProperties(hotelInitializationSettingsReq, hotelInitializationSettings);
            hotelInitializationSettings.setId(SnowflakeIdWorker.nextId());
            hotelInitializationSettings.setCreateId(user.getId());
            hotelInitializationSettings.setGmtCreate(LocalDateTime.now());
            hotelInitializationSettings.setIsDeleted(NOT_DELETE);
            this.baseMapper.insert(hotelInitializationSettings);
            //新增上架渠道
            if(hotelInitializationSettingsReq.getHotelInitializationSettingsChannelReq().size()>0){
                //保存渠道信息
                for (int i = 0; i < hotelInitializationSettingsReq.getHotelInitializationSettingsChannelReq().size(); i++) {
                    HotelInitializationSettingsChannel hotelInitializationSettingsChannel = EntityUtil.copy(hotelInitializationSettingsReq.getHotelInitializationSettingsChannelReq().get(i), HotelInitializationSettingsChannel.class);
                    hotelInitializationSettingsChannel.setId(SnowflakeIdWorker.nextId());
                    hotelInitializationSettingsChannel.setSettingId(hotelInitializationSettings.getId());
                    if(hotelInitializationSettingsChannel.getChannelName().equals("自營門市")||hotelInitializationSettingsChannel.getChannelName().equals("代理商")){
                        hotelInitializationSettingsChannel.setChannelType(ONOFF);
                    }else if(hotelInitializationSettingsChannel.getChannelName().equals("第三方平台")||hotelInitializationSettingsChannel.getChannelName().equals("官方平台")){
                        hotelInitializationSettingsChannel.setChannelType(ONLINE);
                    }
                    hotelInitializationSettingsChannel.setIsDeleted(IsDeletedCode.NO.getCode());
                    hotelInitializationSettingsChannel.setGmtCreate(LocalDateTime.now());
                    hotelInitializationSettingsChannelMapper.insert(hotelInitializationSettingsChannel);
                }
            }
        }
        return Result.success();
    }

    /**
     * 查询巴士票api设置
     *
     * @return
     */
    @Override
    public HotelInitializationSettingsDTO queryDetail(String supplierId) {

        //通过供应商id查询api设置
        List<HotelInitializationSettings> hotelInitializationSettingsList = this.baseMapper.selectList(new QueryWrapper<HotelInitializationSettings>().lambda()
                .eq(StringUtils.isNotBlank(supplierId), HotelInitializationSettings::getSupplierId, supplierId)
                .eq(HotelInitializationSettings::getIsDeleted, NOT_DELETE));
        if (CollectionUtils.isNotEmpty(hotelInitializationSettingsList)) {
            HotelInitializationSettings hotelInitializationSettings = hotelInitializationSettingsList.get(0);
            HotelInitializationSettingsDTO hotelInitializationSettingsDTO = new HotelInitializationSettingsDTO();
            BeanUtils.copyProperties(hotelInitializationSettings, hotelInitializationSettingsDTO);
            //根据setting_id找到所有的channel数据，然后添加到busInitializationSettingsDTO
            QueryWrapper<HotelInitializationSettingsChannel> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(HotelInitializationSettingsChannel::getSettingId,hotelInitializationSettings.getId());
            queryWrapper.lambda().eq(HotelInitializationSettingsChannel::getIsDeleted, IsDeletedCode.NO.getCode());
            List<HotelInitializationSettingsChannel> hotelInitializationSettingsChannelList = hotelInitializationSettingsChannelMapper.selectList(queryWrapper);
            List<HotelInitializationSettingsChannelDTO> hotelInitializationSettingsChannelDTO = EntityUtil.copyList(hotelInitializationSettingsChannelList, HotelInitializationSettingsChannelDTO.class);
            hotelInitializationSettingsDTO.setHotelInitializationSettingsChannelReq(hotelInitializationSettingsChannelDTO);
            return hotelInitializationSettingsDTO;
        }
        return null;
    }

}
